﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.IO;

namespace G.Reader
{
    [DataContract]
    public class Settings : NotifyObject
    {
        private double fontSize = 20;
        [DataMember]
        public double FontSize
        {
            get
            {
                return fontSize;
            }
            set
            {
                if (fontSize != value)
                {
                    fontSize = value;
                    this.NotifyPropertyChanged("FontSize");
                }
            }
        }

        #region 背景图
        private ImageSource _image = null;
        private const String _imagePath = "AppSetting/background.img";
        public ImageSource BackgroundImage
        {
            get
            {
                if (this._image == null)
                {
                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!myIsolatedStorage.FileExists(_imagePath))
                        {
                            return null;
                        }
                        using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(_imagePath, FileMode.Open, FileAccess.Read))
                        {
                            var bitimg = new BitmapImage();
                            bitimg.SetSource(fileStream);
                            _image = bitimg;
                        }
                    }
                }
                return this._image;
            }
        }
        public void SetBackgroundImage(System.IO.Stream imgStream)
        {
            this._image = null;

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!myIsolatedStorage.DirectoryExists("AppSetting"))
                    myIsolatedStorage.CreateDirectory("AppSetting");
                if (myIsolatedStorage.FileExists(_imagePath))
                {
                    myIsolatedStorage.DeleteFile(_imagePath);
                }
                if (imgStream != null)
                {
                    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(_imagePath))
                    {
                        imgStream.CopyTo(fileStream);
                    }
                }
            }
            this.NotifyPropertyChanged("BackgroundImage");
        }
        #endregion


        private MapSettings mapSettings;
        /// <summary>
        /// 地图部分设置
        /// </summary>
        [DataMember]
        public MapSettings MapSettings
        {
            get
            {
                if (mapSettings == null)
                    mapSettings = new MapSettings();
                return mapSettings;
            }
            set
            {
                if (!object.ReferenceEquals(mapSettings, value))
                {
                    mapSettings = value;
                    this.NotifyPropertyChanged("MapSettings");
                }
            }
        }

    }

    [DataContract]
    public class MapSettings : NotifyObject
    {
        private double zoomLevel = 1;
        /// <summary>
        /// 地图缩放级别
        /// </summary>
        [DataMember]
        public double ZoomLevel
        {
            get
            {
                return zoomLevel;
            }
            set
            {
                if (zoomLevel != value)
                {
                    zoomLevel = value;
                    this.NotifyPropertyChanged("ZoomLevel");
                }
            }
        }

        //private double lastCenterLongitude;
        /// <summary>
        /// 最近一次地图中心点经度
        /// </summary>
        [DataMember]
        public double LastCenterLongitude
        {
            get;
            set;
            //get
            //{
            //    return lastCenterLongitude;
            //}
            //set
            //{
            //    if (lastCenterLongitude != value)
            //    {
            //        lastCenterLongitude = value;
            //        this.NotifyPropertyChanged("LastCenterLongitude");
            //    }
            //}
        }
        /// <summary>
        /// 最近一次地图中心点纬度
        /// </summary>
        [DataMember]
        public double LastCenterLatitude
        {
            get;
            set;
        }
    }

}
